package com.example.petplatform.controller;

import com.example.petplatform.common.Result;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Users;
import com.example.petplatform.service.UsersService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;

    @GetMapping("/admin")
    public Result getAdmin() {
        List<Users> list = usersService.getAdmin();
        return Result.success(list);
    }

    // 分页模糊查询
    @GetMapping("/search")
    public Result getSearch(Params params) {
        PageInfo<Users> list = usersService.getSearch(params);
        return Result.success(list);
    }

    @PostMapping("/addadmin")
    public Result addAdmin(@RequestBody Users user) {
        if (user.getId() == null) {
            // 添加数据
            List<Users> list = usersService.addAdmin(user);
            return Result.success(list);
        } else {
            // 修改数据
            List<Users> list = usersService.updateAdmin(user);
            return Result.success(list);
        }
    }

    @DeleteMapping("/delete")
    public Result deleteAdmin(@RequestBody Users user) {
        List<Users> list = usersService.deleteAdmin(user.getId());
        return Result.success(list);
    }
}
